package com.augurit.gis.common.dao;

import java.math.BigDecimal;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.augurit.gis.common.util.DateUtil;



/**
 * <p>Title: DAO的数据转换对象</p>
 * <p>Description: 支持将数据集转换为各种列表</p>
 * <p>Company: 广州奥格智能科技有限公司</p>
 * @author 沈坚丰
 * @date 2009-05-04
 */
public class DataConventor {

	/**
	 * 将结果集转化为String列表
	 * @param resultSet
	 * @return
	 * @throws SQLException
	 */
	public static List<String> resultSet2StringList(ResultSet resultSet) throws SQLException{
		
		if(resultSet==null)
		return null;
		List<String> arrays = new ArrayList<String>();
		while(resultSet.next()){
			String value = resultSet.getString(1);
			arrays.add(value);
		}
		return arrays;
	}
	
	/**
	 * 将结果集转化为HashMap列表
	 * @param resultSet
	 * @return
	 * @throws SQLException
	 */
	@SuppressWarnings("unchecked")
	public static List resultSet2HashMapList(ResultSet resultSet) throws SQLException {
		List list = new ArrayList();
		ResultSetMetaData metaData = resultSet.getMetaData();
		int columnCount = metaData.getColumnCount();
//		System.out.println(columnCount);
		while (resultSet.next()) {
			HashMap hashMap = new HashMap();
			for (int i = 1; i <= columnCount; i++) {
				String sqlType = metaData.getColumnTypeName(i);
				String columnName = metaData.getColumnName(i).toUpperCase();
				String className = metaData.getColumnClassName(i);
				if(resultSet.getString(i)==null){
					hashMap.put(columnName,null);
				}else{
					if (sqlType.indexOf("CHAR") != -1||className.equals(String.class.getName())) {
						hashMap.put(columnName,resultSet.getString(i));
					} 
					else if (sqlType.equals("NUMBER")
							||sqlType.toUpperCase().indexOf("INT")!=-1
							||className.equals(Integer.class.getName())
							||className.equals(Double.class.getName())) {
						hashMap.put(columnName,new Double(resultSet.getDouble(i)));
					}
					else if(sqlType.equals("DATE")
							||className.equals(Timestamp.class.getName())){
//						hashMap.put(columnName,resultSet.getDate(i));
						hashMap.put(columnName,resultSet.getTimestamp(i));
					}
					else if(sqlType.equals("CLOB")){
						hashMap.put(columnName,resultSet.getClob(i));
					}
				}
				
			}
			list.add(hashMap);
		}
		return list;
	}
	
	/**
	 * 将结果集转化为HashMap列表,且HashMap中的对象都为String类型
	 * @param resultSet
	 * @return
	 * @throws SQLException
	 */
	@SuppressWarnings("unchecked")
	public static List resultSet2StringHashMapList(ResultSet resultSet) throws SQLException {
		List list = new ArrayList();
		ResultSetMetaData metaData = resultSet.getMetaData();
		int columnCount = metaData.getColumnCount();
		while (resultSet.next()) {
			HashMap hashMap = new HashMap();
			for (int i = 1; i <= columnCount; i++) {
				String sqlType = metaData.getColumnTypeName(i);
				String columnName = metaData.getColumnName(i).toUpperCase();
				String className = metaData.getColumnClassName(i);
				if(resultSet.getString(i)==null){
					hashMap.put(columnName,"");
				}else{
				   if(sqlType.equals("DATE")||className.equals(Timestamp.class.getName())){
						hashMap.put(columnName,DateUtil.date2ChinaString(resultSet.getTimestamp(i)));
					}
					else if(sqlType.equals("CLOB")){
						hashMap.put(columnName,resultSet.getClob(i).toString());
					}else{
						hashMap.put(columnName,resultSet.getString(i));
					}
				}
				
			}
			list.add(hashMap);
		}
		return list;
	}
	
	/**
	 * 将结果集转化为HashMap列表
	 * @param resultSet
	 * @return
	 * @throws SQLException
	 */
	@SuppressWarnings("unchecked")
	public static List resultSet2HashMapListV2(ResultSet resultSet) throws SQLException {
		List list = new ArrayList();
		ResultSetMetaData metaData = resultSet.getMetaData();
		int columnCount = metaData.getColumnCount();
//		System.out.println(columnCount);
		while (resultSet.next()) {
			HashMap hashMap = new HashMap();
			for (int i = 1; i <= columnCount; i++) {
				//String sqlType = metaData.getColumnTypeName(i);
 				String columnName = metaData.getColumnName(i).toUpperCase();
				String className = metaData.getColumnClassName(i);
				if(resultSet.getString(i)==null){
					hashMap.put(columnName,null);
				}else{
					if (className.equals(String.class.getName())) {
						hashMap.put(columnName,resultSet.getString(i));
					} 
					else if (className.equals(BigDecimal.class.getName())) {
						hashMap.put(columnName,resultSet.getBigDecimal(i));
					}
					else if(className.equals(Timestamp.class.getName())){
						hashMap.put(columnName,resultSet.getDate(i));
					}
					else if(className.equals(Clob.class.getName())){
						hashMap.put(columnName,resultSet.getClob(i));
					}else if(className.equals(Blob.class.getName())){
						hashMap.put(columnName,resultSet.getBlob(i));
					}
				}
				
			}
			list.add(hashMap);
		}
		return list;
	}
	
}
